﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using E4RPDevExpress10;
using RestaurantCommon;

namespace Restaurant.Controls
{
    public partial class RequestsUserControl : UserControl, IGridView
    {
        private Controller _controller;

        #region Properties

        public Controller Controller
        {
            set
            {
                _controller = value;
                _controller.RequestSaved += delegate { SetGridDataSource(); };
                _controller.TableSaved += delegate { SetupTableData(); };
                SetupTableData();
                SetGridDataSource();                
            }
        }
        
        #endregion

        public RequestsUserControl()
        {
            InitializeComponent();
            FormatGridColumns();
            gridView.DoubleClick += GridViewDoubleClick;
            gridView.OptionsView.ShowFooter = true;
        }

        #region Private Methods

        private void SetupTableData()
        {
            mesaDataSet.Clear();
            mesaDataSet.Merge(_controller.Tables());
            tableRepositoryItemLookUpEdit.DataSource = mesaDataSet.Mesa;
            tableRepositoryItemLookUpEdit.DisplayMember = mesaDataSet.Mesa.NomeColumn.ToString();
            tableRepositoryItemLookUpEdit.ValueMember = mesaDataSet.Mesa.IdColumn.ToString();
            tableRepositoryItemLookUpEdit.Columns.Clear();
            tableRepositoryItemLookUpEdit.Columns.AddRange(new[] {
                new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Nome", "Nome", 70, FormatType.None, "", true, HorzAlignment.Near)});
        }

        private void SetGridDataSource()
        {
            PedidoDataSet dataSet = _controller.Requests();
            gridControl.DataSource = null;
            gridControl.DataSource = dataSet;
            gridControl.DataMember = dataSet.Pedido.TableName;
            FormatGridColumns();            
        }

        private void FormatGridColumns()
        {
            DevExpressHelper.SetColumnAsCurrency(colValor);
            DevExpressHelper.SetColumnAsCurrency(colValorPago);
            var gridColumns = new List<GridColumn> { colValor, colValorPago };
            DevExpressHelper.ShowGroupSummaryAsCurrency(gridView, gridColumns);
        }

        #endregion

        void GridViewDoubleClick(object sender, EventArgs e)
        {
            if (!_controller.ExistsOpenedRequest(this, "Deseja mesmo abrir o pedido?"))
                return;
            var estimate = (PedidoDataSet.PedidoRow)gridView.GetDataRow(gridView.FocusedRowHandle);
            if (estimate == null)
                return;
            _controller.ShowRequest(estimate.Id);
        }

        #region IGridView

        public GridView GridView()
        {
            return gridView;
        }

        public string ListName
        {
            get { return "Restaurant:Requests"; }
        }

        #endregion
    }
}
